<template>
  <!-- 底部 -->
  <div class="my-footer">
    <!-- 全选 -->
    <div class="custom-control custom-checkbox">
      <input type="checkbox" class="custom-control-input" id="footerCheck" v-model="isAll">
      <label class="custom-control-label" for="footerCheck">全选</label>
    </div>
    <!-- 合计 -->
    <div>
      <span>合计:</span>
      <span class="price">¥ {{ allPrice }}</span>
    </div>
    <!-- 按钮 -->
    <button type="button" class="footer-btn btn btn-primary">结算 ( {{ allCount }} )</button>
  </div>
</template>

<script>
export default {
  props:{
    list:Array
  },
  //计算属性 控制小选全选
  computed:{
    isAll:{
        set(val){
            console.log(val);  // val就是表单内选框的值(true/false)
            //全选框状态true/false遍历给所有小选框
            this.list.forEach(item => item.goods_state = val)
        },
        get(){
          // 判断数组内小选框状态有没有不符合勾选的条件  影响全选框
            return this.list.every(item => item.goods_state)
        }
    },
    //计算属性 用累加器计算总计并返回
    allPrice(){
        return this.list.reduce((sum,item) =>{
          //判断选框是否被选中
            if(item.goods_state){
               sum += item.goods_price * item.goods_count  
            }
            //返回值
            return sum
        },0).toFixed(2)  //取两位小数
    },
     //计算属性 用累加器计算总数并返回
    allCount(){
        return this.list.reduce((sum,item) =>{
          //判断选框是否被选中
            if(item.goods_state){
               sum += item.goods_count  
            }
            //返回值
            return sum
        },0)
    }
  }
}
</script>

<style lang="less" scoped>
.my-footer {
  position: fixed;
  z-index: 2;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
  background: #fff;

  .price {
    color: red;
    font-weight: bold;
    font-size: 15px;
  }
  .footer-btn {
    min-width: 80px;
    height: 30px;
    line-height: 30px;
    border-radius: 25px;
    padding: 0;
  }
}
</style>